#
# Application.py
# (c) 2007 Honeytoast Interactive
#

from optparse import OptionParser

from PyQt4.QtCore import Qt, QObject, SIGNAL
from PyQt4.QtGui import QApplication

from model import Comic, ComicLoader, Library

from MainWindow import MainWindow
from ComicViewer import ComicViewer

class Application:
    Title = "OpenTome"
    Version = "0.0.0"

    """
    The Application class. This class presents the entry point to the application,
    performing commandline parsing and initialisation.
    """
    def __init__( self ):
        self.application = QApplication( [] )
        QObject.connect( self.application, SIGNAL("lastWindowClosed()"), self.shutdown )
        
    def process( self, args ):
        """
        Process commandline arguments and enter the main loop.
        """
        # Parse the commandline arguments
        parser = OptionParser( "usage: %prog [options] files" )
        parser.add_option( "-f", "--file", help="load a single file" )
        (options, args) = parser.parse_args()
        
        # Create the MainWindow
        self.main_window = MainWindow()
        self.main_window.show()

        # Load a Comic if provided on the commandline
        if options.file:
            c = ComicViewer( Comic(options.file), self.main_window, Qt.Window )
            c.show()

        # Enter the main loop
        self.application.exec_()
        
    def shutdown( self ):
        """
        Clean up on exit.
        """
        ComicLoader.stop_job_queues()
